﻿
using QLKS2014.BLL;
using QLKS2014.PUBLIC;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QLKS2014.GUI.StupidContent
{
    /// <summary>
    /// màn hình cho thuê phòng
    /// </summary>
    public partial class RentingRoom : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }
        ObservableCollection<PhongPublic> _ocPhong; //=> Danh sach nhung phong con trong

        public ObservableCollection<PhongPublic> OcPhong
        {
            get { return _ocPhong; }
            set
            {
                _ocPhong = value;
                OnPropertyChanged("OcPhong");
            }
        }

        int _nextMaPhieuThue;

        ObservableCollection<LoaiKhachHangPublic> _ocLoaiKhach;

        public ObservableCollection<LoaiKhachHangPublic> OcLoaiKhach
        {
            get { return _ocLoaiKhach; }
            set { _ocLoaiKhach = value;
            OnPropertyChanged("OcLoaiKhach");
            }
        }

        ObservableCollection<ChiTietPhieuThuePhongPublic> _ocCTPhieuThuePhong;

        public ObservableCollection<ChiTietPhieuThuePhongPublic> OcCTPhieuThuePhong
        {
            get { return _ocCTPhieuThuePhong; }
            set { _ocCTPhieuThuePhong = value;
            OnPropertyChanged("OcCTPhieuThuePhong");
            }
        }

        private void _insertToCTPhieuThuePhong(ChiTietPhieuThuePhongPublic ctPhieuThuePhong_)
        {
            OcCTPhieuThuePhong.Add(ctPhieuThuePhong_);
            OnPropertyChanged("OcCTPhieuThuePhong");
        }
        

        public RentingRoom()
        {
            InitializeComponent();
            Loaded += RentRoom_Loaded;
        }

        /// <summary>
        /// hien thi danh sach nhung phong chua thue
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RentRoom_Loaded(object sender, RoutedEventArgs e)
        {
            _initialize();
        }

        private void _initialize()
        {
            // init Phong
            //List<PhongPublic> lPhong = new List<PhongPublic>();
            List<PhongPublic> lPhong = new PhongBll().selectPhongEmpty();
            if (lPhong != null)
                OcPhong = new ObservableCollection<PhongPublic>(lPhong);
            else
                OcPhong = new ObservableCollection<PhongPublic>();

            // init loai khach hang
            List<LoaiKhachHangPublic> lLoaiKhachHang = new LoaiKhachHangBll().selectAll(); 
            if (lPhong != null)
                OcLoaiKhach = new ObservableCollection<LoaiKhachHangPublic>(lLoaiKhachHang);
            else
                OcLoaiKhach = new ObservableCollection<LoaiKhachHangPublic>();


            // init chi tiet phieu thue
            OcCTPhieuThuePhong = new ObservableCollection<ChiTietPhieuThuePhongPublic>();

            // init ma phieu thue tiep theo
            _nextMaPhieuThue = _getNextMaPhieuThue(); // => loi !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            // init cbb
            if (cbbRoom.HasItems)
                cbbRoom.SelectedIndex = 0;
            if (cbbCustomerType.HasItems)
                cbbCustomerType.SelectedIndex = 0;
                
        }

        private int _getNextMaPhieuThue()
        {
            return 0;
        }

        /// <summary>
        /// goi ham addNewRentRoom dr luu lai phieu thue phong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _savePhieuThuePhong();

        }

        private void _savePhieuThuePhong()
        {
            if (_checkSaveInput())
            {
                // 1. insert phieu thue phong

                PhieuThuePhongPublic phieuThuePhong0 = new PhieuThuePhongPublic();
                phieuThuePhong0.MaPhieuThue = _nextMaPhieuThue;
                phieuThuePhong0.MaPhong = int.Parse(cbbRoom.SelectedValue.ToString());
                phieuThuePhong0.NgayThue = DateTime.Now;
                phieuThuePhong0.SoLuong = OcCTPhieuThuePhong.Count;
                // insert new data to PhieuThuePhong Table 
                int maPhieuThue = 0; // ????
                new PhieuThuePhongBll().insert(phieuThuePhong0); // ??? => just temp code and wait for canh fix bug

                // 2. đánh dấu phòng đã thuê
                PhongPublic phong0 = new PhongPublic();
                phong0.TenPhong = cbbRoom.Text;
                new PhongBll().update_Day(phong0);


                // 2. insert ct phieu thue phong

                // 2.a: get ma phieu thue phòng
                List<PhieuThuePhongPublic> lPhieuThue = new PhieuThuePhongBll().selectAll();
                if (lPhieuThue == null||lPhieuThue.Count==0)
                    maPhieuThue = 1;
                else
                {
                    PhieuThuePhongPublic phieuThueTmp = lPhieuThue.ElementAt(lPhieuThue.Count - 1);
                    maPhieuThue = phieuThueTmp.MaPhieuThue;
                }

                // 2b. insert chi tiet phieu thue phong
                foreach (ChiTietPhieuThuePhongPublic item in OcCTPhieuThuePhong)
                {
                    item.MaPhieuThue = maPhieuThue;
                    new ChiTietPhieuThuePhongBll().insert(item);
                }

                _initialize(); // reload

            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin!");
            }
        }

        private bool _checkSaveInput()
        {
            return !(OcCTPhieuThuePhong.Count <= 0||String.IsNullOrEmpty(cbbRoom.Text));
        }


        /// <summary>
        /// xoa mot khach hang trong phieu thue phong truoc khi luu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Delete)
            //{
            //    try
            //    {
            //        customer.Remove((CustomerDetail)listCustomer.SelectedItem);
            //        listCustomer.Items.Remove(listCustomer.SelectedItem);
            //    }
            //    catch
            //    {
            //        MessageBox.Show("Thao tác bị lỗi", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            //    }
            //}
        }

        /// <summary>
        /// kiem tra nhap so
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtbCartId_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("^[0-9]+");
            bool match = regex.IsMatch(e.Text);
            e.Handled = !match;
        }

        /// <summary>
        /// xoa thong tin tren form
        /// </summary>
        public void clear()
        {
            //this.tbAdress.Text = "";
            //this.lbDate.Content = DateTime.Now.ToString();
            //this.tbIdCard.Text = "";
            //this.tbName.Text = "";
            //try
            //{
            //    this.listCustomer.Items.Clear();
            //    customer.Clear();
            //}
            //catch { }

        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Enter)
            //{
            //    try
            //    {
            //        if (string.IsNullOrEmpty(this.lbDate.Content.ToString()) ||
            //    string.IsNullOrEmpty(this.cbRoom.SelectedItem.ToString()))
            //        {
            //            MessageBox.Show("Hãy điền đầy đủ thông tin", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
            //            return;
            //        }
            //        try
            //        {
            //            dataRentRoom.AddNewRentRoom(this.cbRoom.SelectedItem.ToString(), this.lbDate.Content.ToString(), customer);
            //        }
            //        catch
            //        { }
            //        this.clear();
            //    }
            //    catch
            //    {

            //    }
            //}
        }

        private void cbbRoom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtbName.Focus();
        }

        private void dgCustomer_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }

        private void dgCustomer_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btnAddCustomer_Click(object sender, RoutedEventArgs e)
        {
            _addCustomer();
        }

        private void _addCustomer()
        {
            if(_checkAddCustomerInput())
            {
                ChiTietPhieuThuePhongPublic ctPhieuThuePhong = new ChiTietPhieuThuePhongPublic();
                ctPhieuThuePhong.CMND = txtbCartId.Text;
                ctPhieuThuePhong.DiaChi = txtbAdress.Text;
                ctPhieuThuePhong.MaLoaiKhachHang = int.Parse(cbbCustomerType.SelectedValue.ToString());
                ctPhieuThuePhong.TenLoaiKhachHang = cbbCustomerType.Text;
                ctPhieuThuePhong.MaPhieuThue = _nextMaPhieuThue;
                ctPhieuThuePhong.TenKhachHang = txtbName.Text;

                // insert a ctPhieuThuePhong To Phieu Thue Phong
                _insertToCTPhieuThuePhong(ctPhieuThuePhong);
            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin!");
            }
            
        }

        private bool _checkAddCustomerInput()
        {
            return !(String.IsNullOrEmpty(txtbName.Text) || String.IsNullOrEmpty(txtbCartId.Text) || String.IsNullOrEmpty(txtbAdress.Text));
        }
    }
}
